<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2020/11/18
 * Time: 11:28
 */
class serv_rapi_es extends L {

    public $node = [
        '193.112.113.247:9200',//节点
    ];

    public $client;

    public function __construct(){
        $this->client = Elasticsearch\ClientBuilder::create()->setHosts($this->node)->build();
    }

    /**
     * 描述 : 创建索引
     * 作者 : Zoro.Zhu
     */
    public function addIndex($index){
        $params = [
            'index' => $index, //索引名称
            'body' => [
                'settings'=> [ //配置
                    'number_of_shards'=> 2,//主分片数
                    'number_of_replicas'=> 1 //主分片的副本数
                ],
                'mappings'=> [  //映射
                    'type' => [
                        'properties' => [
                            'salesPrice' => [
                                'type' => 'float'
                            ],
                            'freightRevenue' => [
                                'type' => 'float'
                            ],
                            'platPrice' => [
                                'type' => 'float'
                            ],
                            'costPrice' => [
                                'type' => 'float'
                            ],
                            'shipFee' => [
                                'type' => 'float'
                            ],
                            'gross' => [
                                'type' => 'float'
                            ],
                            'paymentTime' => [
                                'type' => 'datetime'
                            ],
                            'printTime' => [
                                'type' => 'datetime'
                            ],
                            'lastTimeStr' => [
                                'type' => 'datetime'
                            ],
                        ],
                    ],

                ],
            ]
        ];
        return $this->client->indices()->create($params);
    }

    /**
     * 描述 : 插入一条数据
     * 作者 : Zoro.Zhu
     */
    public function insert($index, $id, $body = []){
        $params = [
            'index' => $index,
            'id'    => $id,
            'body'  => $body
        ];
        return $this->client->index($params);
    }

    /**
     * 描述 : 删除数据
     * 作者 : Zoro.Zhu
     */
    public function delete($index, $id){
        $params = [
            'index' => $index,
            'id'    => $id,
        ];
        return $this->client->delete($params);
    }

    /**
     * 描述 : ES定义的结构进行查询
     * 作者 : Zoro.Zhu
     */
    public function search($index,$query=[]){
        $params = [
            'index' => $index,
            'size' => 20,
            'body' => [
                'query' => $query
            ]
        ];
        return $this->client->search($params);
    }

    /**
     * 描述 : 根据ID获取数据
     * 作者 : Zoro.Zhu
     */
    public function esGet($index, $id){
        $params = [
            'index' => $index,
            'id' => $id
        ];
        return $this->client->get($params);
    }

    /**
     * 描述 : 通过sql语句查询es
     * 作者 : Zoro.Zhu
     */
    public function sqlEs($sql){
        return $this->client->sql()->query(['body' => ['query'=>$sql]]);
    }

    /**
     * 描述 : sql转换es定义的查询结构
     * 作者 : Zoro.Zhu
     */
    public function getSqlEs($sql){
        return $this->client->sql()->translate(['body' => ['query'=>$sql]]);
    }

}
return true;